package de.slothsoft.commons.html;

public class HtmlDocument {

    private StringBuilder sb = new StringBuilder();

    public HtmlDocument() {
	this(null);
    }

    public HtmlDocument(String string) {
	if (string != null) {
	    append(string);
	}
    }

    /**
	 * 
	 */

    public void append(String string, HtmlStyle... styles) {
	this.sb.append(toHtml(string, styles));
    }

    /**
	 * 
	 */

    public void appendLine(String string, HtmlStyle... styles) {
	this.sb.append(toHtml(string, styles) + HtmlTags.LINE_BREAK);
    }

    /**
	 * 
	 */

    public void appendParagraph(String string, HtmlStyle... styles) {
	this.sb.append(HtmlTags.PARAGRAPH_START + toHtml(string, styles)
		+ HtmlTags.PARAGRAPH_END);
    }

    /**
	 * 
	 */

    public void appendUnorderedList(String... strings) {
	appendList(HtmlTags.UNORDERED_LIST_START, HtmlTags.UNORDERED_LIST_END,
		strings);
    }

    /**
	 * 
	 */

    public void appendOrderedList(String... strings) {
	appendList(HtmlTags.ORDERED_LIST_START, HtmlTags.ORDERED_LIST_END,
		strings);
    }

    /**
	 * 
	 */

    private void appendList(String startTag, String endTag, String... strings) {
	this.sb.append(startTag);
	for (String string : strings) {
	    this.sb.append(HtmlTags.LIST_START + toHtml(string)
		    + HtmlTags.LIST_END);
	}
	this.sb.append(endTag);
    }

    /**
	 * 
	 */

    @SuppressWarnings("nls")
    private String toHtml(String string, HtmlStyle... styles) {
	String styledString = string;
	if (styles != null) {
	    for (HtmlStyle style : styles) {
		styledString = style.getStartTag() + styledString
			+ style.getEndTag();
	    }
	}
	return styledString.replace("\n\r", HtmlTags.LINE_BREAK)
		.replace("\n", HtmlTags.LINE_BREAK)
		.replace("\r", HtmlTags.LINE_BREAK)
		.replace("" + (char) 13, HtmlTags.LINE_BREAK);
    }

    /**
	 * 
	 */

    public String getString() {
	return HtmlTags.HTML_START + this.sb.toString() + HtmlTags.HTML_END;
    }
}
